﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rong.EasyExcel;
using Rong.EasyExcel.Models;
using Simple.Services.System.FileInfo.Models;
using Simple.Services.System.Log.Models;
using Simple.Services.System.Office;
using Simple.Services.System.Office.Model;

namespace Simple.Services;

public class LogOperatingService
{
    private readonly SimpleDbContext _context;
    private readonly ExcelOperationService _excelOperationService;
    public LogOperatingService(SimpleDbContext context, ExcelOperationService excelOperationService)
    {
        _context = context;
        _excelOperationService = excelOperationService;
    }

    public async Task<PageResultModel<LogOperatingModel>> GetPageAsync(LogPageInputModel input)
    {
        var result = new PageResultModel<LogOperatingModel>();
        var query = _context.Set<SysLogOperating>().AsQueryable();

        // 根据条件查询
        if (!string.IsNullOrEmpty(input.Name))
        {
            query = query.Where(l => EF.Functions.Like(l.Name!, $"%{input.Name}%"));
        }
        if (!string.IsNullOrEmpty(input.Success))
        {
            bool isSuccess = input.Success == "Y";
            query = query.Where(l => l.IsSuccess == isSuccess);
        }
        if (input.SearchBeginTime.HasValue)
        {
            query = query.Where(l => l.OperatingTime >= input.SearchBeginTime);
        }
        if (input.SearchEndTime.HasValue)
        {
            query = query.Where(l => l.OperatingTime <= input.SearchEndTime);
        }

        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        query = query.OrderByDescending(l => l.Id).Page(input.PageNo, input.PageSize);
        var logs = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<LogOperatingModel>>(logs);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

    public async Task<PageResultModel<LoginLogModel>> GetLoginPageAsync(LoginLogPageInputModel input)
    {
        var result = new PageResultModel<LoginLogModel>();
        var query = BuildQueryCondition(input);

        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        query = query.Page(input.PageNo, input.PageSize);
        var logs = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<LoginLogModel>>(logs);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

    private IQueryable<SysLoginLogs> BuildQueryCondition(LoginLogPageInputModel input)
    {
        var query = _context.Set<SysLoginLogs>().AsQueryable();

        // 根据条件查询
        if (!string.IsNullOrEmpty(input.Name))
        {
            query = query.Where(l => l.Account == input.Name);
        }
        if (!string.IsNullOrEmpty(input.Success))
        {
            bool isSuccess = input.Success == "Y";
            query = query.Where(l => l.IsSuccess == isSuccess);
        }
        if (input.SearchBeginTime.HasValue)
        {
            query = query.Where(l => l.OperatingTime >= input.SearchBeginTime);
        }
        if (input.SearchEndTime.HasValue)
        {
            query = query.Where(l => l.OperatingTime <= input.SearchEndTime);
        }
        query = query.OrderByDescending(l => l.OperatingTime);
        return query;
    }

    /// <summary>
    /// 导出登录日志
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<FileDownInputModel> ExportLoginLogs(LoginLogPageInputModel input)
    {
        var query = BuildQueryCondition(input);
        var logs = await query.ToListAsync();
        var exportLogs = MapperHelper.Map<List<LoginLogExportModel>>(logs);
        return await _excelOperationService.ExportAsync(exportLogs, ExportType.LoginLog);
    }

    /// <summary>
    /// 导入测试
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    public async Task<ImportExcelResultModel<LoginLogImportTestModel>> ImportLoginLogsTest(IFormFile file)
    {
        var importData = await _excelOperationService.ImportAsync<LoginLogImportTestModel>(file);
        if (importData.InvalidList == null || !importData.InvalidList.Any())
        {
            // 业务操作
            // 1.验证数据正确性，例如code查库转名称
            // 2.底层有个bug， [Range]、[RegularExpression]等必须有[Required]，如果可空但是要验证数据范围，就手动验证



            // 数据正确导入后置为true
            importData.IsSuccess = true;
        }
        return importData;
    }

 }
